/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl.demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.business.dao.demo.DemoMapper;
import com.rf.business.model.demo.Demo;
import com.rf.business.model.demo.DemoExample;
import com.rf.business.service.demo.DemoService;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;

@Service("demoService")
public class DemoServiceImpl implements DemoService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private DemoMapper demoMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param DemoId
	 * @return Demo
	 */
   	@Override
	public Demo getDemoByID(String id) throws Exception{
		// TODO Auto-generated method stub
		return demoMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据Demo获取数据
	 * @param Demo
	 * @return List<Demo>
	 */
	@Override
	public List<Demo> getDemo(Demo Demo) throws Exception{
		// TODO Auto-generated method stub
		DemoExample example = new DemoExample();
		List<Demo> list = demoMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<Demo>
	 */
	@Override
	public List<Demo> getDemo() throws Exception{
		// TODO Auto-generated method stub
		DemoExample example = new DemoExample();
		return demoMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param Demo
	 * @return
	 */
	@Override
	public void addDemo(Demo Demo) throws Exception{
		// TODO Auto-generated method stub
		Demo.setId(UuidUtil.get32UUID());
		demoMapper.insert(Demo);
	}

	/**
	 * 修改数据
	 * @param Demo
	 * @param
	 */
	@Override
	public void updateDemo(Demo Demo) throws Exception{
		// TODO Auto-generated method stub
		demoMapper.updateByPrimaryKeySelective(Demo);
	}
	
	/**
	 * 删除数据
	 * @param DemoId
	 * @param
	 */
	@Override
	public void deleteDemo(String id
) throws Exception{
		// TODO Auto-generated method stub
		demoMapper.deleteByPrimaryKey(id
);
	}
	
	
//	/**
//	 * 删除数据
//	 * @param userId
//	 * @param
//	 */
//	@Override
//	public void deleteDemo(String ids) throws Exception{
//		// TODO Auto-generated method stub
//		String sql = "update demo set state = "+ConfigUtils.USERDELSTATE +" where id in("+ids+")";
//		sqlAdapterMapper.updateSQL(sql);
//	}

	/**
	 * 获取数据分页
	 * @param Demo
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getDemoPage(Demo Demo,PageUtils pageUtils) throws Exception{
		DemoExample example = new DemoExample();
		com.rf.business.model.demo.DemoExample.Criteria criteria= example.createCriteria();
		if(Demo.getName()!=null && !Demo.getName().equals("")){
			criteria.andNameLike(Demo.getName());
		}
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Demo> list = demoMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
}

